package selcoursesys.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import selcoursesys.entity.UserEntity;
import selcoursesys.service.UserService;
import selcoursesys.service.impl.UserServiceImpl;


import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Iterator;
import java.util.List;
/**
 * Created by Lpbdz on 2017/4/6.
 */
public class UserAction extends ActionSupport{
    private String uid=null;
    private String upwd=null;
    private String utype=null;
    private HttpServletRequest request;
    private HttpSession session;

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpSession getSession() {
        return session;
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getUpwd() {
        return upwd;
    }

    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }

    public String getUtype() {
        return utype;
    }

    public void setUtype(String utype) {
        this.utype = utype;
    }

    public String userLogin() throws Exception{
        String errormessage=null;
        request=ServletActionContext.getRequest();
        session=request.getSession();
        UserEntity userEntity=new UserEntity();
        userEntity.setUtype(this.getUtype());
        userEntity.setUid(this.getUid());
        userEntity.setUpwd(this.getUpwd());
        if(this.getUtype()==null||this.getUid()==null||"".equals(this.getUid().trim())||this.getUpwd().trim().length()==0){
            session.setAttribute("utypeempty","存在空的输入");
            session.setAttribute("erroemessage",null);
            return ERROR;
        }
        System.out.println(this.getUid()+" "+this.getUpwd()+" "+this.getUtype());
        UserService userService=new UserServiceImpl();
        boolean returnmessage=userService.userLogin(userEntity);
        if(returnmessage==false){
            session.setAttribute("erroemessage","用户名或密码错误");
            session.setAttribute("utypeempty","null");
            return ERROR;
        }else{
            session.setAttribute("stuid",""+this.getUid());
            switch (this.getUtype()){
                case "学生":
                    session.setAttribute("stutype","no null");
                    session.setAttribute("teatype",null);
                    session.setAttribute("admintype",null);
                    break;
                case "教师":
                    session.setAttribute("stutype",null);
                    session.setAttribute("teatype","no null");
                    session.setAttribute("admintype",null);
                    break;
                case "管理员":
                    session.setAttribute("stutype",null);
                    session.setAttribute("teatype",null);
                    session.setAttribute("admintype","no null");
            }
            ActionContext.getContext().put("stuid",this.getUid());
            return SUCCESS;
        }
    }
}